/******************导游相关路由器***************************/
const express = require('express')
const pool = require('../pool.js')
const router = express.Router()
module.exports = router

//1.分页查询导游的接口
//GET /list
//请求参数：pno=2&count=3
//测试地址：http://127.0.0.1:8080/v1/guide/list?pno=2&count=3
router.get('/list',(req,res,next)=>{
	let obj = req.query
	if(!obj.pno){
		obj.pno = 1
	}
	if(!obj.count){
		obj.count = 5
	}
	let start = (obj.pno-1)*obj.count
	let size = Number(obj.count)
	let sql = 'select guide_name,unit,guide_phone from guide limit ?,?'
	pool.query(sql,[start,size],(err,result)=>{
		if(err){
			return next(err)
		}
		res.send({code:200,msg:'导游列表',data:result})
	})
})

// 2、查询某一个导游的接口
//GET /show
//请求参数gid=5
//测试地址：http://127.0.0.1:8080/v1/guide/show?gid=5
router.get('/show',(req,res,next)=>{
	let id = req.query.gid
	let sql = 'select guide_name,unit,guide_phone from guide where gid='+id
	pool.query(sql,id,(err,result)=>{
		if(err){
			return next(err)
		}
		res.send(result)
	})
})

//3.添加导游的接口
//POST  /add
//请求主体 guide_name=cindy&unit=舟山旅游&guide_phone=17609876543
//测试地址：http://127.0.0.1:8080/v1/guide/add
router.post('/add',(req,res,next)=>{
	let name = req.body.guide_name
	let unit = req.body.unit
	let phone = req.body.guide_phone
	if(!/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone)){
		res.send({code:201,msg:'电话格式错误！'})
		return
	}
	if(!name){
		res.send({code:202,msg:'姓名不能为空！'})
	}
	let sql = 'insert into guide(guide_name,unit,guide_phone) values(?,?,?)'
	pool.query(sql,[name,unit,phone],(err,result)=>{
		if(err){
			return next(err)
		}
		res.send({code:200,msg:'添加成功！'})
	})
})